package cn.itcast.converter;

import cn.itcast.enums.CollegeStatusEnum;
import cn.itcast.enums.DegreeEnum;
import cn.itcast.enums.GenderEnum;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;


public class EnumConverter implements Converter<Integer> {
    
    @Override
    public Class<?> supportJavaTypeKey() {
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, 
                                    ExcelContentProperty contentProperty,
                                    GlobalConfiguration globalConfiguration) {
        // 获取列名
        String fieldName = contentProperty.getField().getName();
        String stringValue = cellData.getStringValue();
        
        // 根据字段名进行不同的转换
        switch (fieldName) {
            case "gender":
                return GenderEnum.getCodeByDesc(stringValue);
            case "degree":
                return DegreeEnum.getCodeByDesc(stringValue);
            case "isCollege":
                return CollegeStatusEnum.getCodeByDesc(stringValue);
            default:
                return null;
        }
    }

    @Override
    public WriteCellData<?> convertToExcelData(Integer value, 
                                              ExcelContentProperty contentProperty,
                                              GlobalConfiguration globalConfiguration) {
        // 导出时使用，导入可不实现
        return new WriteCellData<>(value.toString());
    }
}